require 'calendar_grid'

class WeekendPlugin
  def initialize(day)
    @day = day
  end
  def day_weekend?
    @day.weekend % 3 == 0
  end
end

class Calendar
  
  def initialize(sprint)
    @sprint = sprint
    months = 0
    if @sprint.start_date.day < 10
      months =1
    else
      months =2
    end
    cg = CalendarGrid::Builder.new(@sprint.start_date,months)
    @cal = cg.build do |day|
       WeekendPlugin.new day
    end
  end
  
  # this method allows us to delegate calls directly to our @cal object.
  # instead of Calendar.cal.years you can do Calendar.years!  Crazy Ruby Magic!
  def method_missing(symbol, *args)
    @cal.respond_to?(symbol) ? @cal.send(symbol, *args) : super
  end

  def to_html
    
    html = "<h2>Calendar from #{@cal.first.strftime("%D")} to #{@cal.last.strftime("%D")}</h2>"

    @cal.years.each do |y|
      html << "<h3>#{y.year.to_s}</h3>\n"
      y.months.each do |m|
        html << m.strftime("%B")
        m.weeks.collect do |w|
          w.collect { |day| html << "#{(day.proxy?) ? '* ' : day.strftime("%d")}" }
        end
        html <<"<p/>"
        puts html
      end
    end    
  end

  def dosomething
   
    puts "Calendar from #{@cal.first.strftime("%D")} to #{@cal.last.strftime("%D")}"

    @cal.years.each do |y|
      puts y.year
      y.months.each do |m|
        puts m.strftime("%B")
        s = m.weeks.collect do |w|
          w.collect { |day| "#{(day.proxy?) ? '* ' : day.strftime("%d")}" }.join(" ")
        end
        puts s
        puts
      end
    end
  end
end



